<template>
  <div class="double-sidebar">
    <!-- 一级侧边栏 - 图标导航 -->
    <div class="primary-sidebar">
      <div class="logo-container">
        <router-link to="/" class="logo-link">
          <el-icon class="logo-icon"><Monitor /></el-icon>
        </router-link>
      </div>
      
      <div class="menu-categories">
        <div
          v-for="category in menuCategories"
          :key="category.id"
          class="category-item"
          :class="{ 'active': activeCategory === category.id }"
          @click="setActiveCategory(category.id)"
          @mouseenter="showTooltip(category, $event)"
          @mouseleave="hideTooltip"
        >
          <div class="category-icon" :style="{ backgroundColor: category.color }">
            <el-icon><component :is="category.icon" /></el-icon>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 二级侧边栏 - 详细菜单 -->
    <div 
      class="secondary-sidebar"
      :class="{ 'show': sidebar.opened, 'hide': !sidebar.opened }"
    >
      <div class="secondary-header">
        <div class="category-info">
          <div class="category-icon-large" :style="{ backgroundColor: activeCategoryInfo?.color }">
            <el-icon><component :is="activeCategoryInfo?.icon" /></el-icon>
          </div>
          <div class="category-details">
            <h3 class="category-name">{{ activeCategoryInfo?.title }}</h3>
            <p class="category-desc">{{ activeCategoryInfo?.description }}</p>
          </div>
        </div>
      </div>
      
      <div class="secondary-menu">
        <el-scrollbar>
          <el-menu
            :default-active="activeMenu"
            :background-color="'transparent'"
            :text-color="'#606266'"
            :active-text-color="'#409eff'"
            mode="vertical"
            router
          >
            <template v-for="route in filteredRoutes" :key="route.path">
              <sidebar-item 
                :item="route" 
                :base-path="route.path"
              />
            </template>
          </el-menu>
        </el-scrollbar>
      </div>
    </div>
    
    <!-- 悬浮提示 -->
    <div
      v-if="tooltip.show"
      class="tooltip"
      :style="{ top: tooltip.top + 'px', left: tooltip.left + 'px' }"
    >
      <div class="tooltip-content">
        <h4>{{ tooltip.title }}</h4>
        <p>{{ tooltip.description }}</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useAppStore } from '@/stores/app'
import { menuCategories, routes } from '@/router'
import SidebarItem from './SidebarItem.vue'
import { 
  Monitor, Setting, School, Notebook, Document
} from '@element-plus/icons-vue'

const route = useRoute()
const appStore = useAppStore()

// 状态
const activeCategory = ref('dashboard')
const tooltip = ref({
  show: false,
  title: '',
  description: '',
  top: 0,
  left: 0
})

// 计算属性
const sidebar = computed(() => appStore.sidebar)

const activeCategoryInfo = computed(() => {
  return menuCategories.find(cat => cat.id === activeCategory.value)
})

const activeMenu = computed(() => {
  const { meta, path } = route
  if (meta?.activeMenu) {
    return meta.activeMenu
  }
  return path
})

const filteredRoutes = computed(() => {
  console.log('当前活动分类:', activeCategory.value)
  
  const filtered = routes.filter(route => {
    // 跳过登录页和隐藏路由
    if (route.path === '/login' || (route.meta && route.meta.hidden)) {
      return false
    }

    // 根据分类过滤路由
    if (route.meta && route.meta.category === activeCategory.value) {
      console.log('匹配一级路由:', route.path, '分类:', route.meta.category)
      return true
    }

    // 检查子路由的分类
    if (route.children) {
      const hasMatchingChild = route.children.some(child => {
        const isMatch = child.meta && child.meta.category === activeCategory.value
        if (isMatch) {
          console.log('匹配子路由:', route.path + '/' + child.path, '分类:', child.meta.category)
        }
        return isMatch
      })
      if (hasMatchingChild) {
        return true
      }
    }

    // 特殊处理：仪表盘分类显示首页
    if (activeCategory.value === 'dashboard' && route.path === '/') {
      console.log('匹配仪表盘首页')
      return true
    }

    return false
  })
  
  console.log('过滤后的路由:', filtered.map(r => r.path))
  return filtered
})

// 方法
const setActiveCategory = (categoryId) => {
  activeCategory.value = categoryId
  // 如果侧边栏是收起状态，则自动展开
  if (!sidebar.value.opened) {
    appStore.openSidebar()
  }
  hideTooltip()
}

const showTooltip = (category, event) => {
  const rect = event.currentTarget.getBoundingClientRect()
  tooltip.value = {
    show: true,
    title: category.title,
    description: category.description,
    top: rect.top,
    left: rect.right + 10
  }
}

const hideTooltip = () => {
  tooltip.value.show = false
}

// 根据当前路由设置活动分类
const setActiveCategoryFromRoute = () => {
  console.log('当前路径:', route.path)
  // 首先，检查子路由是否精确匹配当前路径
  for (const r of routes) {
    if (r.children) {
      const matchingChild = r.children.find(child => {
        const fullChildPath = r.path === '/' ? `/${child.path}` : `${r.path}/${child.path}`
        console.log('检查子路由路径:', fullChildPath, '与当前路径:', route.path)
        return route.path === fullChildPath || route.path.startsWith(`${fullChildPath}/`)
      })
      
      if (matchingChild && matchingChild.meta?.category) {
        console.log('匹配到子路由:', matchingChild.path, '分类:', matchingChild.meta.category)
        activeCategory.value = matchingChild.meta.category
        return
      }
    }
  }
  
  // 其次，检查一级路由是否匹配当前路径
  const currentRoute = routes.find(r => {
    return route.path === r.path || route.path.startsWith(`${r.path}/`)
  })
  
  if (currentRoute?.meta?.category) {
    console.log('匹配到一级路由:', currentRoute.path, '分类:', currentRoute.meta.category)
    activeCategory.value = currentRoute.meta.category
  }
}

// 监听路由变化，更新活动分类
watch(() => route.path, () => {
  setActiveCategoryFromRoute()
})

onMounted(() => {
  setActiveCategoryFromRoute()
})
</script>

<style lang="scss" scoped>
.double-sidebar {
  display: flex;
  height: 100vh;
  position: relative;
}

.primary-sidebar {
  width: 60px;
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
  display: flex;
  flex-direction: column;
  transition: width 0.3s ease;
  position: relative;
  z-index: 1001;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
  
  .logo-container {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding: 0 8px;
    
    .logo-link {
      display: flex;
      align-items: center;
      color: #fff;
      text-decoration: none;
      
      .logo-icon {
        font-size: 24px;
      }
    }
  }
  
  .menu-categories {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 16px 0;
    overflow-y: auto;
    
    .category-item {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 12px 0;
      margin: 4px 8px;
      cursor: pointer;
      position: relative;
      transition: all 0.3s ease;
      border-radius: 8px;
      
      &:hover {
        background-color: rgba(255, 255, 255, 0.1);
        
        .category-icon {
          transform: translateY(-2px);
        }
      }
      
      &.active {
        background-color: rgba(255, 255, 255, 0.15);
        
        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          height: 100%;
          width: 3px;
          background-color: #fff;
          border-radius: 0 3px 3px 0;
        }
        
        .category-icon {
          transform: scale(1.1);
          box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
      }
      
      .category-icon {
        width: 32px;
        height: 32px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;
        
        .el-icon {
          font-size: 18px;
          color: #fff;
        }
      }
    }
  }
}

.secondary-sidebar {
  width: 200px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  position: relative;
  
  &.show {
    width: 200px;
    opacity: 1;
    transform: translateX(0);
  }
  
  &.hide {
    width: 0;
    opacity: 0;
    transform: translateX(-20px);
  }
  
  .secondary-header {
    padding: 16px;
    border-bottom: 1px solid #ebeef5;
    
    .category-info {
      display: flex;
      align-items: center;
      
      .category-icon-large {
        width: 40px;
        height: 40px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 12px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        
        .el-icon {
          font-size: 20px;
          color: #fff;
        }
      }
      
      .category-details {
        flex: 1;
        
        .category-name {
          font-size: 16px;
          font-weight: 600;
          margin: 0 0 4px;
          color: #303133;
        }
        
        .category-desc {
          font-size: 12px;
          color: #909399;
          margin: 0;
          line-height: 1.4;
        }
      }
    }
  }
  
  .secondary-menu {
    flex: 1;
    overflow: hidden;
    
    .el-scrollbar {
      height: 100%;
      
      .el-menu {
        border-right: none;
        width: 100%;
        padding: 0;
      }
    }
  }
}

.tooltip {
  position: fixed;
  z-index: 2000;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  padding: 12px;
  width: 200px;
  pointer-events: none;
  animation: fadeIn 0.2s ease;
  
  .tooltip-content {
    h4 {
      margin: 0 0 6px;
      font-size: 14px;
      font-weight: 600;
      color: #303133;
    }
    
    p {
      margin: 0;
      font-size: 12px;
      color: #606266;
      line-height: 1.4;
    }
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .secondary-sidebar {
    width: 180px;
    
    &.show {
      width: 180px;
    }
  }
}

@media (max-width: 480px) {
  .secondary-sidebar {
    position: absolute;
    left: 60px;
    height: 100%;
    z-index: 1000;
  }
}
</style>


